
<template>
<theme-box title="商户结算账户添加">
    <form class="form-inline" @submit.prevent v-el:form>
        <div class="box-body">
            <form-group title="商户号" :required="true">
                <select name="mchtId" class="form-control" v-model="mchtIdValue" style="width:265px;max-width: 265px">
                    <option v-for="mcht in mchtList" :value="mcht.value">
                        {{mcht.key}}
                    </option>
                </select>
            </form-group>
            <form-select title="结算通道" :required="true" name="merWay" :options="typeOptions"></form-select>
            <form-input title="支付商户id" :required="true" name="merId"></form-input>
            <form-input title="支付key" :required="true" name="merKey"></form-input>
        </div>
        <!-- /.box-body -->
        <div class="box-footer">
            <v-button type="reset" @click="reset">重置</v-button>
            <v-button type="submit" theme="primary" class="pull-right">确认</v-button>
        </div>
        <!-- /.box-footer -->
    </form>
</theme-box>
</template>

<script>
import 'plugins/jquery-validation';
import {
    dateTime
} from 'ui/collections/general.js'

var serialize = require('form-serialize');

export default {
    data: () => ({
        mchtList: [],
        typeOptions:[
            {key: "益码通",value: "ebjPay"},
            {key: "易生",value: "easyPay"},
        ],
    }),
    methods: {
        reset() {
            $("label.error").remove();
            this.$els.form.reset()
        },
        getMcht(data) {
            let self = this;
            self.$post({
                url: "/api/v2" + projectName_Url + "/list/mcht/sett",
                method:"GET"
            }).then(response => {
                let list = response.content.info;
                self.$set("mchtList", list);
                self.mchtIdValue = list[0].value
            }, error => {});
        }
    },
    ready() {
        dateTime()
        var self = this;
        self.getMcht();
        $(this.$els.form).validate({
            submitHandler: async function(form) {
                var _data = serialize(this.currentForm, {
                    hash: true,
                    empty: true
                });
                var result = await self.$post({
                    url: "/api/v2" + projectName_Url + "/plat/mchtSett/add",
                    data: _data,
                    method:"POST"
                })
                let data = {
                    title: "操作成功",
                    content: "是否进入商户结算账户列表查看?"
                }
                Midway.confirm(data).then(function() {
                    self.$route.router.go('/merSett/list');
                })
                self.reset();
            },
            errorPlacement: function(error, element) {
                if (element.attr("name") == "file") {
                    error.insertAfter(".file-item");
                } else {
                    error.insertAfter(element);
                }
            },
            rules: {
                mchtId: {
                    required: true
                },
                merWay:{
                    required: true
                },
                merId: {
                    required: true,
                    int:true,
                    min:1,
                    stringMaxLength: 20
                },
                merKey: {
                    required: true,
                    stringMaxLength: 50
                }
            }
        });
    }
}
</script>
